var rootPath = getRootPath();

$(function() {
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	jQuery.validator.addMethod("checkIncreaseIntegral", function(value, element) {
		var increaseIntegral = $("#increaseIntegral").val();
		if(increaseIntegral.indexOf(".") != -1) {
			return false;
		}
		return true;
	}, "请输入整数增减积分数");
	
	$("#increaseIntegral").bind("change keydown up",function(){
        if(this.value.indexOf(".") != -1) {
        	$(this).val("");
        }
    })
	
	/* 字数限制 */
	$('#comment').bind("change keydown up", function() {
		$(this).val($(this).val().substring(0, 200));
	});
	
	$("#form-userIntegral-edit").validate({
		rules:{
			integral:{
				number:true
			},
			increaseIntegral:{
				required:true,
				number:true,
				min:-10000,
				max:99999,
				maxlength:7,
				checkIncreaseIntegral:true
			},
			itid:{
				required:true
			},
			comment:{
				required:true,
				maxlength:200
			}
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			var uid = $("#uid").val();
			var integral = $("#integral").val() == "" ? null : parseInt($("#integral").val());
			var increaseIntegral = parseInt($("#increaseIntegral").val());
			if(integral == null ? (increaseIntegral < 0) : (integral + increaseIntegral < 0)) {
				layer.alert("减少的积分数不能超过原积分数");
				return;
			}
			var itid = $("#itid").val();
			var comment = $("#comment").val();
			if(comment == null || comment == '') {
				layer.alert("请输入内容");
				return;
			}
			$.ajax({
				url: rootPath + '/user/updateUserIntegral',
	            type: 'POST',
	            data: JSON.stringify({
		        	"uid" : uid,
		        	"integral" : integral,
	            	"increaseIntegral" : increaseIntegral,
	            	"itid" : itid,
	            	"comment" : comment
		        }),
	            contentType: 'application/json;charset=UTF-8',
	            success: function (data) {
	            	if("0000" == data.returnCode) {
	            		layer.msg('成功更新用户积分',{icon:1,time:1000},function() {
	            			var index = parent.layer.getFrameIndex(window.name);
	            			parent.$("#searchUserIntegral").click();
	            			parent.layer.close(index);
	            		});	            		
	            	} else {
	            		layer.alert(data.returnMessage);
	            	}
	            },
	            error: function (jqXHR, textStatus, errorThrown) {
	            	layer.msg('error:)',{icon:1,time:1000});
	            }
	        });
		}
	});	
});